<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use \think\Controller;
use \Page;
use think\helper\Time;
class Index extends CommonController
{
    public function index()
    {
        // $data=db('user')->where('Number',getCookie())->select();
        $this->assign('user',getUser());
        return $this->fetch();
    }
    //欢迎
    public function welcome(){
    	return $this->fetch('welcome');
    }
    //用户管理
    public function user_info(){
       
        $data=Db::table('user')->order('user_id asc')->select();
        $count=db('user')->count('user_id');
        $this->assign('count',$count);
        // $this->assign('list',$data);
        $p = new Page($data,20);
        // var_dump($p);
        $this->assign('list',$p);
        return $this->fetch('user_info');
        
    }
    //update
    public function update($id)
    {
        // var_dump($id);
        $db = Db::table('user');
        $num=input('number');
        $user = input('name');
        $password = input('password');
        $remarks=input('remarks');
        $upd = $db->where(['user_id'=>$id])->update(['user'=>$user,'password'=>$password,'Number'=>$num,'remarks'=>$remarks]);
        if($upd){
            echo "修改成功";
            $this->redirect('user_info');
        }else{
            $this->error('修改失败','user_info');
        }
    }
    //delete
    public function delete($id)
    {
        $db = Db::table('user');
        $del=$db->where(['user_id' => $id])->delete();
        $this->redirect('user_info');
    }
    //insert
    public function insert()
    {
        $db = Db::table('user');
        $num=input('number');
        $user = input('name');
        $password = input('password');
        $remarks=input('remarks');
        $insert=$db->insert(['user'=>$user,'password'=>$password,'Number'=>$num,'remarks'=>$remarks]);
        if($insert){
            $this->success('添加成功!','user_info');
        }else{
            $this->error('添加错误!','user_info');
        }
    }
//--------------------------消息管理-------------------------------------------------------
    //消息发布
    public function news_release()
    {
        
        return $this->fetch('news_release');
    }
    //消息发布insert
    public function news_insert()
    {
        $db = Db::table('page');
        $page_title=input('title');
        if(request()->isPost()){
            
            $page_content = input('post.editor');
            $content= strip_tags($page_content);
            // var_dump($conters);
            
        }
        $time = date("Y-m-d H:i:s");
        $user_id='0';//管理员身份
        //正则得到图片的路径
        $pattern1= '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
        $m1=$m2=array();
            
        preg_match_all($pattern1,$page_content,$m1);
        //多个图片路径拼接成一个字符用 ，隔开
        //去掉末尾多余的
        if (preg_match_all($pattern1,$page_content,$m1)!=0) {
        $imges=$m1[1][0];
        }
        else
        {
          $imges=null;
        }
        $insert=$db->insert(['page_title'=>$page_title,'page_content'=>$page_content,'page_time'=>$time,'user_id'=>$user_id,'page_image'=>$imges,'content'=>$content]);
        if($insert){
            $this->success('添加成功!','news_release');
        }else{
            $this->error('添加错误!','news_release');
        }
    }
    //消息操作
    public function info_action()
    {   
        $count=db('page')->count('page_id');
        $this->assign('count',$count);
        $data=db('page')->select();
        $p=new Page($data,20);
        $this->assign('list',$p);
        // var_dump($data);
        return $this->fetch('info_action');
    }
    //发布消息修改
    public function info_change($id)
    {
        $data=db('page')->where('page_id',$id)->select();
        // var_dump($data);
        $this->assign('data',$data[0]);
        return $this->fetch('info_change');
    }
    public function info_change_update()
    {
        $db = Db::table('page');
        $page_title=input('title');
         $page_id=input('page_id');
         // var_dump($page_id);
        if(request()->isPost()){
            
            $page_content = input('post.editor');
            $content= strip_tags($page_content);
            // var_dump($conters);
            
        }
        $time = date("Y-m-d H:i:s");
        $user_id='0';//管理员身份
        //正则得到图片的路径
        $pattern1= '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*?>/i';
        $m1=$m2=array();
            
        preg_match_all($pattern1,$page_content,$m1);
        //多个图片路径拼接成一个字符用 ，隔开
        //去掉末尾多余的
        if (preg_match_all($pattern1,$page_content,$m1)!=0) {
        $imges=$m1[1][0];
        }
        else
        {
          $imges=null;
        }
        $insert=$db->where('page_id',$page_id)->update(['page_title'=>$page_title,'page_content'=>$page_content,'page_time'=>$time,'page_image'=>$imges,'content'=>$content]);
        if($insert){
            $this->success('修改成功!','info_action');
        }else{
            $this->error('修改失败!','info_action');
        }
    }
    //
    public function info_change_delete($id)
    {
        $delete=db('page')->where('page_id',$id)->delete();
        if($delete){
            $this->success('修改成功!','info_action');
        }else{
            $this->error('修改失败!','info_action');
        }
    }
    public function feedback_list()
    {
        return $this->fetch('feedback_list');
    }
    public function comment()
    {
        return $this->fetch('comment');
    }
    //奥运奖牌
    public function gold()
    {
        $data=db('gold')->select();
         $count=db('gold')->order('id asc')->count('name');
         $p = new Page($data,20);
         $this->assign(['count'=>$count,'list'=>$p]);

        return $this->fetch('gold');
    }
    //金牌insert
    public function insert_gold()
    {
        $name=input('name');
        $gold=input('gold');
        $silver=input('silver');
        $copper=input('copper');
        $time=input('time');
        $data=db('gold')->insert(['name'=>$name,'gold'=>$gold,'silver'=>$silver,'copper'=>$copper,'time'=>$time]);
        if($data){
            $this->success('添加成功!','gold');
        }else{
            $this->error('添加错误!','gold');
        }
    }
    //金牌
    public function update_gold($id)
    {
        $name=input('name');
        $gold=input('gold');
        $silver=input('silver');
        $copper=input('copper');
        $time=input('time');
        $data=db('gold')->where('id',$id)->update(['name'=>$name,'gold'=>$gold,'silver'=>$silver,'copper'=>$copper,'time'=>$time]);
        if($data){
            $this->success('添加成功!','gold');
        }else{
            $this->error('添加错误!','gold');
        }
    }
    public function delete_gold($id)
    {
        $data=db('gold')->where('id',$id)->delete();
        if($data){
            $this->success('添加成功!','gold');
        }else{
            $this->error('添加错误!','gold');
        }
    }
    public function check($time,$num){
        $condition['page_time'] = array('like',"%".$time."%");
        if($time&&$num)
        {      
            $res = Db::table('page')->alias('p')
            ->join('user u','p.user_id=u.user_id')
            ->where('u.number',$num)
            ->where($condition)
            ->select();
            // ->where['page_time']=array('like',"%".$time."%")->select();
        }
        else if(!$time)
        {
            $res = Db::table('page')->alias('p')
            ->join('user u','p.user_id=u.user_id')
            ->where('u.number',$num)
            ->select();
        }
        else{
            $res = Db::table('page')->alias('p')
            ->join('user u','p.user_id=u.user_id')
            ->where($condition)
            // ->where['page_time']=array('like',"%".$time."%")
            ->select();
        }
        return json($res);
    }
    public function check_del($id){
        $data = (int)$id;
        $res = Db::execute("delete from page where page_id = $data");
        // return $res;
    }
    //个人信息
    public function person_info(){
    $Number=Db::table('user')->where('Number', getCookie())->select();
    $this->assign('Number',$Number);
       // var_dump($tea_id);
    return $this->fetch('person_info');

    }

    public function update_info(){
        $db = Db::table('user');
        $Number=input('Number');
        $user= input('user');
        $password = input('password');
        $updata=$db->where(['Number'=>$Number])->update(['Number'=>$Number,'user'=>$user,'password'=>$password]);

       // $updata= $db->where(['user_id'=>$user_id])->update(['Number'=>$Number,'user'=>$user,'password'=>$password]);
       //var_dump($Number);
     //var_dump($user);
      //var_dump($password);

         if($updata){
             $this->success('修改成功!','Index/person_info');
         }else{
            $this->error('修改失败!','Index/person_info');
         }
        
        //return $;
    }
}
